﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using WP.RM.DataEntity;
using WP.RM.DataAccess;
using WP.RM.BusinessRule;
using WP.DB;
using WP.Commons;

namespace WP.RM.BusinessLogical
{
    public class RMProductBLL : IBusinessLogical
    {

         RMProductDAL  rmProductDAL = new RMProductDAL();

         public RMProductBLL()
         {
         }

        #region For implement interface

        #region Select Record by RECID OverLoad +1
        public RMProductDEL  SelectDataByRecID(DBAccess dBAccess, string strRecId)
        {
            RMProductDEL  rmProductDEL = new RMProductDEL();

            DataFunctions.SelectByRecID(dBAccess, rmProductDEL, rmProductDAL.Fields, strRecId);

            return rmProductDEL;
        }
        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            RMProductDEL rmProductDEL = null;

            try
            {
                rmProductDEL = SelectDataByRecID(dBAccess, strRecId);
                return rmProductDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region public bool Insert()
        public bool Insert(IDataEntity iRMProductDEL)
        {
            bool bReturn = true;
            RMProductDEL rmProductDEL = iRMProductDEL as RMProductDEL;
            DBAccess dBAccess = null;

            dBAccess = new DBAccess(false, true);
            try
            {
                bReturn = Insert(dBAccess, rmProductDEL);

                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                bReturn = false;
            }
            return bReturn;
        }
        public bool Insert(DBAccess dBAccess, RMProductDEL rmProductDEL)
        {
            bool bFlag = true;
            RMProductBRL rmProductBRL = new RMProductBRL();

            if (rmProductBRL.InsertValid(dBAccess, rmProductDEL))
            {
                DataFunctions.InsertTable(dBAccess, rmProductDAL.Fields, rmProductDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

       

        #region public bool Delete()
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            bool bFlag = false;

            try
            {
                if (new RMProductBRL().DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    DataFunctions.DeleteByRecID(dBAccess, "rmProduct", strRecId);

                    dBAccess.CommitTranAndCloseCon();

                    bFlag = true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }

            return bFlag;
        }
        #endregion

        #region public bool Update()
        public bool Update(IDataEntity iRMProductDEL)
        {
            bool bReturn = true;
            RMProductDEL rmProductDEL = iRMProductDEL as RMProductDEL;
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Update(dBAccess, rmProductDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }

            return bReturn;
        }
        public bool Update(DBAccess dBAccess, RMProductDEL  rmProductDEL)
        {
            bool bFlag = true;
            RMProductBRL rmProductBRL = new RMProductBRL();

            if (rmProductBRL.UpdateValid(dBAccess, rmProductDEL))
            {
                DataFunctions.UpdateTable(dBAccess, rmProductDAL.Fields, RMProductDEL.TABLENAME_RMProduct  , rmProductDEL.Tables[0].Rows[0]);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

        #endregion

    }
}
